// 强制登录，onLoad时当前页面出栈，不返回当前页，适用于而页面全部功能需要登录的场景
export function forceLogin(vuex_token) {
	if (!vuex_token) {
		uni.redirectTo({
			url: '/pages/login/login'
		})
	}
	return !vuex_token
}

// 校验登录，调用方法时当前页面不出栈，返回当前页，适用于页面部分功能需要登录的场景
export function toLogin(vuex_token) {
	if (!vuex_token) {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}
	return !vuex_token
}

export function getCurrentRoute() {
	const routes = getCurrentPages() // 获取当前打开过的页面路由数组
	const curRoute = routes[routes.length - 1].route // 获取当前页面路由
	const curParam = routes[routes.length - 1].options // 获取路由参数
	// 拼接参数
	let param = ''
	for (const key in curParam) {
		param += '&' + key + '=' + curParam[key]
	}
	return "/" + curRoute + param.replace('&', '?')
}

export function openURL(url) {
	// 非http请求直接跳转本地页面
	if (!/^https?/.test(url)) {
		uni.navigateTo({
			url: url
		})
		return
	}
	// #ifdef APP-PLUS || MP
	// Http请求用webview打开
	uni.navigateTo({
		url: '/pages/container/webview',
		success: function(res) {
			// 第一步：通过eventChannel向被打开页面传送数据
			res.eventChannel.emit('acceptDataForWebview', {
				data: url
			})
		}
	})
	return
	// #endif
	// #ifdef APP-PLUS
	plus.runtime.openURL(url)
	// #endif
	// #ifdef H5
	window.open(url)
	// #endif
	// #ifdef MP
	uni.setClipboardData({
		data: url
	})
	uni.showModal({
		content: '已自动复制网址，请在手机浏览器里粘贴该网址',
		showCancel: false
	})
	// #endif
}

export function formatNumToKw(num) {
	// 数值简化，千转k，万转w
	return num >= 1e3 && num < 1e4 ? (num / 1e3).toFixed(2) + 'k' : num >= 1e4 ? (num / 1e4).toFixed(2) + 'w' : num
}

export function getSuffix(filename) {
	// 获取文件后缀
	const pos = filename.lastIndexOf('.')
	let suffix = ''
	if (pos !== -1) {
		suffix = filename.substring(pos)
	}
	return suffix
}

export function contactKefu(phoneNum) {
	// 联系电话
	uni.makePhoneCall({
		phoneNumber: phoneNum || '17688889999' //仅为示例
	});
}

function getCurrentTime() {
  let keep = ''
  let date = new Date()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '' + m + '' + d + '' + h + '' + f + '' + s
  return keep // 20161064134947
}
/*uniapp附带的方法*/
function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function (milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function (dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function (number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};


export default {
	forceLogin,
	toLogin,
	getCurrentRoute,
	openURL,
	formatNumToKw,
	getSuffix,
	contactKefu,
	getCurrentTime,
	formatTime,
	formatLocation,
	dateUtils
}
